package com.service.impl;

import com.mapper.PermissionMapper;
import com.pojo.Owners;
import com.pojo.Permission;
import com.pojo.User;
import com.service.PermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceimpl implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;
    @Override
    public List<String> queryUserPermission() {
        //1.从shiro中取出主体对象
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        List<String> permCodeList=null;
        //2.调用mapper查询该用户具有的权限
if(principal instanceof Owners){
     permCodeList = permissionMapper.selectPermCodeByRoleId(((Owners) principal).getRoleid());
}else if(principal instanceof User){
     permCodeList = permissionMapper.selectPermCodeByRoleId(((User) principal).getRoleid());
}
        return permCodeList;
    }

    @Override
    public List<String> queryownerPermission() {
        //1.从shiro中取出主体对象
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        List<String> permCodeList=null;
        //2.调用mapper查询该用户具有的权限
        if(principal instanceof Owners){
            permCodeList = permissionMapper.selectPermCodeByRoleId(((Owners) principal).getRoleid());
        }else if(principal instanceof User){
            permCodeList = permissionMapper.selectPermCodeByRoleId(((User) principal).getRoleid());
        }
        return permCodeList;
    }

    @Override
    public Map<Permission, List<Permission>> createMenu() {
        //1.创建一个map集合，用来存储用户的权限
        Map<Permission, List<Permission>> map = new HashMap<>();
        //2.先去查询所有的一级权限
        List<Permission> permissionList = permissionMapper.selectPermissionByParentId(null);
        //3.获取主体对象
        Subject subject = SecurityUtils.getSubject();
        if(permissionList.size() > 0){
            //循环所有的一级权限
            for (Permission permission : permissionList) {
                //每次循环一级权限时，创建一个二级权限的集合
                List<Permission> permList = new ArrayList<>();
                //4.判断主体中是否有该权限的code
                if(subject.isPermitted(permission.getPermisCode())){
                    //5.授权判定通过，说明该用户拥有此一级权限，再根据拥有的一级权限去查询所有的二级权限
                    List<Permission> permissionsList2 = permissionMapper.selectPermissionByParentId(permission.getId());
                    for (Permission permission2 : permissionsList2) {
                        //如果二级权限判定能通过
                        if(subject.isPermitted(permission2.getPermisCode())){
                            //将该权限添加到list集合中
                            permList.add(permission2);
                        }
                    }
                    map.put(permission,permList);
                }

                System.out.println("这是permission权限的集合"+permission);
                System.out.println("这是二级权限的集合"+permList);
            }
        }
        System.out.println("这是serviceimpl中的map"+map);
        return map;
    }
}
